// pages/teacher/teacheractivity/teacheractivity.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        adata: {},
        check: true,
        lat: null,
        lng: null,
        len: 0,
        getchecksign: [
            ['签到', '签退'],
            ['10.00m', '50.00m', '100.00m', '200.00m', '300.00m', '400.00m', '500.00m']
        ],
        selectcheck: '',
        selectdistance: '',
    },
    async initCheckBtn(e) {

        var that = this
        var check = this.data.check
        if (check) {
            var index = e.detail.value
            console.log(e.detail.value)
            var selectsign = this.data.getchecksign[0][index[0]]
            var selectdistance = this.data.getchecksign[1][index[1]]
            this.setData({
                selectcheck: selectsign,
                selectdistance: selectdistance,
            })
            this.ruleLocation()
        } else {
            var ano = this.data.adata.ano
            var sql = "UPDATE activity SET signin='N',signout='N' WHERE ano=" + ano
            var pre = await this.sqlRequest(sql).then(function (result) {
                // 在这里对 PromiseResult 进行处理
                // len = result.data.length
                console.log(result.data)
            })
            this.setData({
                check: !check
            })
        }

    },
    /**
     * 授权打开定位
     */
    ruleLocation() {
        var that = this;
        //检查地理位置授权
        wx.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
                    console.log('location', res);
                    // 调起客户端设置界面
                    wx.openSetting({
                        success: function (data) {
                            if (data.authSetting["scope.userLocation"] == true) {
                                //  wx.showToast({
                                //    title: '授权成功',
                                //    icon: 'success',
                                //    duration: 5000
                                //  })
                                that.getLocation();
                            } else {
                                //  wx.showToast({
                                //    title: '授权失败',
                                //    icon: 'success',
                                //    duration: 5000
                                //  })
                            }
                        }
                    })
                } else {
                    that.getLocation();
                }
            }
        })
    },
    /**
     * 调用wx.getLocation系统API,获取并设置当前位置经纬度
     */
    getLocation: function () {
        var that = this;
        //获取位置
        wx.getLocation({
            type: 'gcj02', //默认为 wgs84 返回 gps 坐标，gcj02 返回可用于wx.openLocation的坐标
            success: function (res) {
                //console.log(res.longitude+'~~~'+res.latitude);
                that.setData({
                    lng: res.longitude,
                    lat: res.latitude
                })
                // 处理业务逻辑 （把当前经纬度发送给后端处理等）
                console.log(that.data.lng)
                console.log(that.data.lat)
                that.dealData()
                // var h = that.getDistances(that.data.lat, that.data.lng, that.data.lat + 1, that.data.lng + 1)
                // console.log(h)
            },
            fail: function (e) {
                console.log("error", e);
            }
        });
    },
    joinManBtn(){
        var ano = this.data.adata.ano
        wx.navigateTo({
          url: '/pages/teacher/teacherjoin/teacherjoin?ano='+ano,
        })
    },
    async dealData() {
        var sql = ''
        var check = this.data.check
        var lat = this.data.lat.toFixed(6)
        var lng = this.data.lng.toFixed(6)
        var ano = this.data.adata.ano
        var distance = parseFloat(this.data.selectdistance.replace('m',''))
        if (this.data.selectcheck === '签到') {
            sql = "UPDATE activity SET lat=" + lat + ",lng=" + lng + ",distance=" + distance + ",signin='Y' WHERE ano=" + ano
        } else {
            sql = "UPDATE activity SET lat=" + lat + ",lng=" + lng + ",distance='" + distance + "',signout='Y' WHERE ano=" + ano
        }
        if (check) {

            var pre = await this.sqlRequest(sql).then(function (result) {
                // 在这里对 PromiseResult 进行处理
                // len = result.data.length
                console.log(result.data)
            })
        }
        this.setData({
            check: !check
        })
    },
    async sqlRequest(sql) {
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql
            },
        })
        // console.log(res1)
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        var data = JSON.parse(options.adata)
        this.setData({
            adata: data,
        })
        if (data.signin === 'N' && data.signout === 'N') {
            this.setData({
                check: true,
            })
        } else {
            this.setData({
                check: false,
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        wx.setNavigationBarTitle({
            title: '活动详情',
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})